package com.wei.czz.framework.blog.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.wei.czz.common.enums.CommonEnum;
import com.wei.czz.common.po.UserPo;
import com.wei.czz.common.tool.PageDto;
import com.wei.czz.common.utils.SecurityUtils;
import com.wei.czz.common.vo.blog.audit.group.BlogAuditGroupFormVo;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;

import java.util.*;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;

import com.wei.czz.framework.blog.dao.BlogAuditGroupDao;
import com.wei.czz.framework.blog.entity.BlogAuditGroupEntity;
import com.wei.czz.framework.blog.service.BlogAuditGroupService;
import org.springframework.util.CollectionUtils;

/**
 * Created by IntelliJ IDEA.
 *
 * @author wei
 * date: 2024-12-05 08:21:03
 * className: BlogAuditGroupServiceImpl
 * version: 1.0
 * description:
 */
@Service("blogAuditGroupService")
public class BlogAuditGroupServiceImpl extends ServiceImpl<BlogAuditGroupDao, BlogAuditGroupEntity> implements BlogAuditGroupService {

    private static final Logger log = LoggerFactory.getLogger(BlogAuditGroupServiceImpl.class);

    @Override
    public void saveOrEdit(BlogAuditGroupEntity blogAuditGroup) {
        // 获取操作用户
        String username = SecurityUtils.getOrDefaultUsername(StringUtils.EMPTY);
        Long userId = SecurityUtils.getOrDefaultUserId(CommonEnum.ZERO.getLongValue());
        Date date = new Date();

        if (Objects.isNull(blogAuditGroup.getId())) {
            blogAuditGroup.setDeleteStatus(CommonEnum.ZERO.getValue())
                    .setUpdateTime(date)
                    .setUpdateUser(StringUtils.EMPTY)
                    .setUpdateUserId(CommonEnum.ZERO.getLongValue())
                    .setCreateTime(date)
                    .setCreateUser(username)
                    .setCreateUserId(userId);

            int count = baseMapper.insert(blogAuditGroup);
            log.info("保存博客审核分组完成。count={}", count);
        } else {
            blogAuditGroup.setUpdateTime(date)
                    .setUpdateUser(username)
                    .setUpdateUserId(userId);

            int count = baseMapper.updateById(blogAuditGroup);
            log.info("修改博客审核分组完成。count={}", count);
        }
    }

    @Override
    public List<BlogAuditGroupEntity> getList(List<Long> idList) {
        if (CollectionUtils.isEmpty(idList)) {
            log.info("获取博客审核分组，传递的主键列表为空");
            return Collections.emptyList();
        }

        LambdaQueryWrapper<BlogAuditGroupEntity> blogAuditGroupLambdaQueryWrapper = new LambdaQueryWrapper<>();
        blogAuditGroupLambdaQueryWrapper.in(BlogAuditGroupEntity::getId, idList)
                .eq(BlogAuditGroupEntity::getDeleteStatus, CommonEnum.ZERO.getValue());
        // 查询
        List<BlogAuditGroupEntity> blogAuditGroupList = baseMapper.selectList(blogAuditGroupLambdaQueryWrapper);
        log.info("主键列表查询博客审核分组完成。list.size={}", blogAuditGroupList.size());

        return blogAuditGroupList;
    }

    @Override
    public List<BlogAuditGroupEntity> findList(List<Integer> auditTypeList, List<Integer> auditLevelList) {
        if (CollectionUtils.isEmpty(auditTypeList) && CollectionUtils.isEmpty(auditLevelList)) {
            log.info("获取博客审核分组，传递的审核类型列表和审核级别列表均为空");
            return Collections.emptyList();
        }

        LambdaQueryWrapper<BlogAuditGroupEntity> blogAuditGroupLambdaQueryWrapper = new LambdaQueryWrapper<>();
        blogAuditGroupLambdaQueryWrapper.in(!CollectionUtils.isEmpty(auditTypeList), BlogAuditGroupEntity::getAuditType,
                auditTypeList)
                .in(!CollectionUtils.isEmpty(auditLevelList), BlogAuditGroupEntity::getAuditLevel, auditLevelList)
                .eq(BlogAuditGroupEntity::getDeleteStatus, CommonEnum.ZERO.getValue());
        // 查询
        List<BlogAuditGroupEntity> blogAuditGroupList = baseMapper.selectList(blogAuditGroupLambdaQueryWrapper);
        log.info("查询博客审核分组完成。list.size={}", blogAuditGroupList.size());
        return blogAuditGroupList;
    }

    @Override
    public PageDto<BlogAuditGroupEntity> getBlogAuditGroupPageList(BlogAuditGroupFormVo formVo) {
        // 构造mp分页对象
        Page<BlogAuditGroupEntity> page = new Page<>(formVo.getPage(), formVo.getLimit());

        LambdaQueryWrapper<BlogAuditGroupEntity> blogAuditGroupLambdaQueryWrapper = new LambdaQueryWrapper<>();
        blogAuditGroupLambdaQueryWrapper.eq(Objects.nonNull(formVo.getAuditType()), BlogAuditGroupEntity::getAuditType,
                formVo.getAuditType())
                .eq(Objects.nonNull(formVo.getAuditLevel()), BlogAuditGroupEntity::getAuditLevel, formVo.getAuditLevel())
                .like(StringUtils.isNotBlank(formVo.getWord()), BlogAuditGroupEntity::getName, formVo.getWord())
                .eq(BlogAuditGroupEntity::getDeleteStatus, formVo.getDeleteStatus());
        // 排序设置
        blogAuditGroupLambdaQueryWrapper.orderByDesc(BlogAuditGroupEntity::getUpdateTime);
        // 分页查询
        baseMapper.selectPage(page, blogAuditGroupLambdaQueryWrapper);
        // 结果封装
        return new PageDto<>(page);
    }

    @Override
    public void delete(List<Long> idList) {
        if (CollectionUtils.isEmpty(idList)) {
            log.info("删除博客审核分组，传递的主键列表为空");
            return;
        }
        // 获取请求用户
        UserPo userPo = SecurityUtils.getUser();
        Date date = new Date();

        LambdaUpdateWrapper<BlogAuditGroupEntity> blogAuditGroupLambdaUpdateWrapper = new LambdaUpdateWrapper<>();
        blogAuditGroupLambdaUpdateWrapper.set(BlogAuditGroupEntity::getDeleteStatus, CommonEnum.ONE.getValue())
                .set(BlogAuditGroupEntity::getUpdateTime, date)
                .set(BlogAuditGroupEntity::getUpdateUser, userPo.getUsername())
                .set(BlogAuditGroupEntity::getUpdateUserId, userPo.getUserId());
        blogAuditGroupLambdaUpdateWrapper.in(BlogAuditGroupEntity::getId, idList)
                .eq(BlogAuditGroupEntity::getDeleteStatus, CommonEnum.ZERO.getValue());
        // 删除
        int count = baseMapper.update(blogAuditGroupLambdaUpdateWrapper);
        log.info("删除博客审核分组完成。count={}", count);
    }
}